﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseMethods
{
    public static class Normal
    {

        /// <summary>
        /// </summary>
        /// <param name="random"></param>
        /// <returns></returns>
        public static double NextNormalDouble(this Random random)
        {
            return NextNormalDouble(random, 1, 0);
        }

        /// <summary>
        /// </summary>
        /// <param name="random"></param>
        /// <param name="deviation"></param>
        /// <param name="mean"></param>
        /// <returns></returns>
        public static double NextNormalDouble(this Random random, double deviation, double mean)
        {
            double u;
            double s;
            do
            {
                u = random.NextDouble() * 2 - 1;
                double v = random.NextDouble() * 2 - 1;
                s = u * u + v * v;
            } while (s >= 1);

            return mean + deviation * u * Math.Sqrt(-2.0 * Math.Log(s) / s);
        }

        /// <summary>
        /// </summary>
        /// <param name="random"></param>
        /// <param name="arrayLength"></param>
        /// <param name="deviation"></param>
        /// <param name="mean"></param>
        public static double[] NormalArray(this Random random, int arrayLength, double deviation = 1, double mean = 0)
        {
            var bytes = new double[arrayLength];
            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] = NextNormalDouble(random, deviation, mean);
            }
            return bytes;
        }
    }
}
